<?php

use think\migration\Migrator;
use think\migration\db\Column;

class SyncTakeoutOrderToCouponOrder extends Migrator
{

    public function up()
    {

        $sql = <<<SQL
ALTER TABLE `coupon_order`
MODIFY COLUMN `product_name`  varchar(300) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '产品名称' ;


SQL;

        $this->execute($sql);
        $sql = <<<SQL
ALTER TABLE `coupon_order`
MODIFY COLUMN `title`  varchar(300) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '标题' ;


SQL;

        $this->execute($sql);


        \app\model\TakeoutOrder::query()
            ->where('agent_id', '<>', 0)
            ->whereIn('status', [1, 8])
            ->where('settle_status', 1)
            ->select()->each(function (\app\model\TakeoutOrder $takeoutOrder) {

                $serv_types = [
                    1 => PRODUCT_MEITUAN_TAKEOUT_SERV,
                    2 => PRODUCT_ELE_SERV,
                    3 => PRODUCT_METUAN_SERV,
                ];

                if (empty($serv_types[$takeoutOrder->order_type])) {
                    return;
                }

                $serv_type = $serv_types[$takeoutOrder->order_type];

                $couponOrder = \app\model\CouponOrder::findOrCreate([
                    'supplier_order_sn' => $takeoutOrder->order_id,
                ]);
                /**@var \app\model\BaseSupplier $baseSupplier */
                $baseSupplier = \app\model\BaseSupplier::query()->where('serv_type', $serv_type)->find();


                /**@var \app\model\CouponOrder $couponOrder */
                $couponOrder->supplier_id = $baseSupplier->isEmpty() ? 0 : $baseSupplier->id;
                $couponOrder->supplier_discount = 0;
                $couponOrder->supplier_price = $takeoutOrder->payprice;
                $couponOrder->settle_price = $takeoutOrder->payprice;
                $couponOrder->profit = $takeoutOrder->profit;
                $couponOrder->agent_id = $takeoutOrder->agent_id;
                $couponOrder->settle_type = SETTLE_TYPE_PROFIT;
                $couponOrder->settle_discount = 100;//全返
                $couponOrder->platform_profit = 0;//全返
                $couponOrder->order_id = $takeoutOrder->order_id;
                $couponOrder->out_order_id = $takeoutOrder->order_id;
                $couponOrder->origin_amount = $takeoutOrder->payprice;
                $couponOrder->amount = $takeoutOrder->payprice;
                $couponOrder->pay_amount = $takeoutOrder->payprice;
                $couponOrder->pay_type = 0;

                $couponOrder->pay_status = 1;
                $couponOrder->pay_time = date('Y-m-d H:i:s', $takeoutOrder->paytime);
                $couponOrder->refund_status = 1;
                $couponOrder->refund_amount = 0;
                $couponOrder->user_id = $takeoutOrder->user_id;
                $couponOrder->cnid = $takeoutOrder->channel_id;
                $couponOrder->serv_type = $serv_type;
                $couponOrder->type = 0;
                $couponOrder->settled_at = $this->getSettleDate($couponOrder->pay_time);
                $couponOrder->status = 3;
                $couponOrder->agent_settle_status = 2;
                $couponOrder->product_name = $takeoutOrder->title;
                $couponOrder->log = '迁移';
                $couponOrder->supplier_code = $baseSupplier->code;
                $couponOrder->order_created_at = $couponOrder->pay_time;
                $couponOrder->title = $takeoutOrder->title;
                $couponOrder->sid = $takeoutOrder->sid;

                $couponOrder->save();


            });
    }

    public function down()
    {

    }


    /**
     * 根据支付时间，获取结算时间
     * 结算时间：次月25号
     */
    protected function getSettleDate($payTime)
    {
        $year = substr($payTime, 0, 4);
        $month = substr($payTime, 5, 2) + 1;
        return date('Y-m-d', mktime(0, 0, 0, $month, 25, $year));
    }


}
